package kube

import (
	"context"
	"testing"

	bkeaddon "gopkg.openfuyao.cn/bkecommon/cluster/addon"
	"gopkg.openfuyao.cn/bkecommon/cluster/api/v1beta1"
	confv1beta1 "gopkg.openfuyao.cn/bkecommon/cluster/api/v1beta1"
	bkenode "gopkg.openfuyao.cn/bkecommon/cluster/node"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	bkev1beta1 "gopkg.openfuyao.cn/cluster-api-provider-bke/api/v1beta1"
	"gopkg.openfuyao.cn/cluster-api-provider-bke/utils/config"
)

const (
	kubeconfig = "/home/czy/goland/cluster-api-provider-bke/pkg/kube/kubeconfig.conf"
	repo       = "deploy.bocloud.k8s:40443/kubernetes/"
)

var kubeproxyAddon = v1beta1.Product{
	Name:    "kubeproxy",
	Version: "1.21.1",
	Param: map[string]string{
		"clusterNetworkMode": "calico",
		"podSubnet":          "192.168.0.0/16",
		"kubeConfigDir":      "/etc/kubernetes",
	},
}

var calicoAddon = v1beta1.Product{
	Name:    "calico",
	Version: "v3.14.1",
	Param: map[string]string{
		"ipModVersion": "ipv4",
		"podSubnet":    "192.168.0.0/16",
		"calicoMode":   "bgp",
	},
}

var corednsAddon = v1beta1.Product{
	Name:    "coredns",
	Version: "v1.8.0",
	Block:   true,
	Param: map[string]string{
		"dnsIP":     "10.96.0.10",
		"dnsDomain": "cluster.local",
	},
}

var bocoperaterAddon = v1beta1.Product{
	Name:    "bocoperator",
	Version: "latest",
	Block:   true,
	Param: map[string]string{
		"boc.version": "v3.5",
		//"boc.k8svip":     "172.28.100.250",
		//"boc.k8sport":    "36443",
		//"boc.yumport":    "40080",
		//"boc.repodomain": "deploy.bocloud.k8s",
		//"boc.repoip":     "172.28.8.186",
		//"boc.repoport":   "40443",
		//"boc.yumnodes":   "master-1,worker-1,worker-2",
		//"boc.yumdomain":  "deploy.bocloud.k8s",
		//"boc.repo":       "deploy.bocloud.k8s:40443/paas/",
	},
}

var prometheusAddon = v1beta1.Product{
	Name:    "prometheus",
	Version: "v2.32.1",
	Block:   false,
	Param: map[string]string{
		//"alertmanager.replicas":               1,
		//"alertmanager.ipModVersion":           "ipv4",
		//"kube-state-metrics.ipModVersion":     "ipv4",
		//"node-exporter.ipModVersion":          "ipv4",
		//"prometheus-adapter.apiServerSrcPort": "6443",
		"004-k8s-components-svc.etcdIps": "10.10.20.20",
		"etcdIps":                        "10.10.20.20",
	},
}

var mysqlAddon = v1beta1.Product{
	Name:    "mysql",
	Version: "5.7",
	Block:   true,
	Param: map[string]string{
		"nfsServer": "172.28.8.186",
		"replicas":  "2",
	},
}

var nfsAddon = v1beta1.Product{
	Name:    "nfs-csi",
	Version: "v4.1.0",
	Block:   true,
	Param: map[string]string{
		"nfsServer": "172.28.8.186",
	},
}

var kubesecurity = v1beta1.Product{
	Name:    "kubesecurity",
	Version: "v1.2.0",
	Block:   false,
}

var volcano = v1beta1.Product{
	Name:    "volcano",
	Version: "release-1.7-bcc",
	Block:   false,
}

var kserve = v1beta1.Product{

	Name:    "kserve",
	Version: "v0.11.2",
	Block:   true,
}

var priorityclass = v1beta1.Product{

	Name:    "priorityclass",
	Version: "latest",
	Block:   false,
}

var certmanager = v1beta1.Product{

	Name:    "cert-manager",
	Version: "v1.11.0",
	Block:   true,
}

var gpu = v1beta1.Product{
	Name:    "kube-gpu",
	Version: "1.0.0",
	Block:   false,
}

func TestKubeConfig(t *testing.T) {
	client, err := NewClientFromConfig(kubeconfig)
	if err != nil {
		t.Log(err)
		return
	}
	cs, _ := client.KubeClient()
	// list all nodes
	nodes, err := cs.CoreV1().Nodes().List(context.Background(), v1.ListOptions{})
	if err != nil {
		t.Log(err)
		return
	}
	for _, node := range nodes.Items {
		t.Log(node.Name)
	}
}

func TestInstallAddon(t *testing.T) {
	config.MetricsAddr = "0"
	client, err := NewClientFromConfig(kubeconfig)
	if err != nil {
		t.Log(err)
		return
	}
	cluster := &bkev1beta1.BKECluster{
		Spec: confv1beta1.BKEClusterSpec{
			ClusterConfig: &v1beta1.BKEConfig{
				Cluster: v1beta1.Cluster{
					ImageRepo: v1beta1.Repo{
						Domain: "deploy.bocloud.k8s",
						Ip:     "10.10.21.61",
						Port:   "40443",
						Prefix: "kubernetes",
					},
					KubernetesVersion: "v1.23.17",
				},
				Nodes: []v1beta1.Node{
					{
						Hostname: "cluster",
						Port:     "22",
						Role: []string{
							bkenode.MasterWorkerNodeRole,
							bkenode.EtcdNodeRole,
						},
					},
				},
				Addons:      nil,
				CustomExtra: nil,
			},
		},
	}
	addons := []v1beta1.Product{
		//kubeproxyAddon,
		//calicoAddon,
		//corednsAddon,
		//bocoperaterAddon,
		//prometheusAddon,
		//mysqlAddon,
		//nfsAddon,
		//volcano,
		//certmanager,
		//kserve,
		//priorityclass,
		gpu,
	}
	for _, addon := range addons {
		err = client.InstallAddon(cluster, &bkeaddon.AddonTransfer{Addon: &addon, Operate: bkeaddon.CreateAddon}, nil)
		if err != nil {
			t.Error(err)
		}
	}
}

func TestInstallAddonByToken(t *testing.T) {
	cluster := &bkev1beta1.BKECluster{
		Spec: confv1beta1.BKEClusterSpec{
			ClusterConfig: &v1beta1.BKEConfig{
				Cluster: v1beta1.Cluster{
					ImageRepo: v1beta1.Repo{
						Domain: "deploy.bocloud.k8s",
						Ip:     "172.28.8.237",
						Port:   "40443",
						Prefix: "kubernetes",
					},
				},
				Nodes:       nil,
				Addons:      nil,
				CustomExtra: nil,
			},
		},
	}

	token := "eyJhbGciOiJSUzI1NiIsImtpZCI6InJYYzd5SE4tSEhycTFHWHhOZHlaMXgxdHloOVhFZ3A0U2QyTHR6RVljdlUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJtYW5hZ2VtZW50LWFkbWluLXRva2VuLTlsdDVqIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6Im1hbmFnZW1lbnQtYWRtaW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJmNjhhODAxNS0wYWYyLTRlMmItODQzMC03MGU0YjYwMDQ0YjYiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06bWFuYWdlbWVudC1hZG1pbiJ9.PsfMgjtCcWVYCd-rFF4SdbxKEKUUqzbWkGXzZgeFVPSDcEGjf9tlNCsUJdeMWoWDQBh1-euP0dEt6uVmz_G3bObH9VBrWPScK1ubayL4yZYt6TWPH85npXHtRA0yNOGhr0yrJjCUF6RORfX0QroygGIxK24QDLX74V_mQ3wTW4PCwzqi8hs3wVrcAgCwViGfBv430sT1ucFrFFUDHxmbxBc4UwXTtX_4Ga0E-fchKCRMcYCREtES7-ZusRS3kI0fWH25yV7MoiN2wpan1KvbBaNHRarBS0Dam9gDjSwLy1zG8PeuIKhTVcW5UqLNCYGtjTWu8OYifk2rQ8U9onWFYA"
	host := "10.10.37.51"
	port := "6443"
	client, err := NewClientFromK8sToken(host, port, token)
	if err != nil {
		t.Log(err)
		return
	}

	addons := []v1beta1.Product{
		//kubeproxyAddon,
		//calicoAddon,
		//corednsAddon,
		//bocoperaterAddon,
		prometheusAddon,
		//mysqlAddon,
		//nfsAddon,
		//kubesecurity,
	}

	for _, addon := range addons {
		err = client.InstallAddon(cluster, &bkeaddon.AddonTransfer{Addon: &addon, Operate: bkeaddon.CreateAddon}, nil)
		if err != nil {
			t.Error(err)
		}
	}

}
